package com.easytoolsoft.easyreport.web.controllers;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/*import com.sg.common.http.HttpHelper;
import com.sg.common.utils.Constant;*/


/*import com.sg.common.empty.core.EpUtil;
import com.sg.common.utils.Constant;
*/

/**
 * Reporting控制器
 */
@Controller
@RequestMapping(value = "/")
public class HomeController extends AbstractController {

	/*@RequestMapping(value = { "/login" })
	public String login() {
	return "login";

}*/
	
	/**
	 * 
	 取得当前的账号信息（无论单点与否） 判断账号是否为空 否 跳转到home页面 是 若单点情况跳转到单点服务器登陆页面 若非，则跳转到本地登陆页面
	 * (登陆成功后跳转到home页面)
	 * 
	 * @param request
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	@ResponseBody
	public Object loginPage(HttpServletRequest request)
			throws ServletException, IOException {
		String myCode = EmptyInterceptor.findMyCode(request);
		if (StringUtils.isNotEmpty(myCode)) {
			//request.getRequestDispatcher("/cfg/common/home").forward(request,response);
			
			return new ModelAndView("home");
		}		
			/*ModelAndView modelAndView = new ModelAndView("redirect:"
					+ "http://10.10.39.1:8082/auth/login.sg");*/
			
		ModelAndView modelAndView = new ModelAndView("redirect:"
				+ Constant.get("autologin"));
		
			Map<String, Object> model = modelAndView.getModel();
			//model.put("sName", "easyreport-web");
			model.put("sName", Constant.get("sName"));
			String url = request.getRequestURL().toString();
			// 获取请求的参数字符串
			String queryString = request.getQueryString();
			if (StringUtils.isNotEmpty(queryString))
				url = url + "?" + queryString;
			model.put("returnUrl", java.net.URLEncoder.encode(url, "utf-8"));

			return modelAndView;		
		
	}

	
	
	 @RequestMapping(value = "/report/logout")
		@ResponseBody
		public Object logout(HttpServletRequest request) throws IOException {	
			request.getSession().invalidate();
				Map<String, String> mip = new HashMap<String, String>();
				mip.put("clientIp", request.getRemoteAddr());
				EmptyInterceptor.httpPost(Constant.get("logout"), mip);
				return new ModelAndView("redirect:" + Constant.get("autologout"));
		}	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	@RequestMapping(value = "/flushConf")
	@ResponseBody
	public Object flush(HttpServletRequest request) throws IOException {
		PropertyFileUtil.flushCONF(Constant.PROPERTIES_PATH);
		return "succ";
	}
	

	@RequestMapping(value = { "", "/", "/index" })
		
	
	public String index()  {	
		
		
		return "home";

	}

}